import { useOperationConfiguration } from '@renderer/stores/operationConfiguration'
import http from '@renderer/utils/http'
import { storeToRefs } from 'pinia'
import { Vector3 } from 'three'
export interface UserInfo {
  customerId: string
  customerNo: string
  customerShortName: string
  id: string
  token: string
  username: string
}
export interface PointVos extends Vector3 {
  floorElevation: number
  leftDistance: number
  rightDistance: number
  roofElevation: number
  tunnelHeight: number
}

export interface WireItem extends Vector3 {
  customerNo: string
  pointVos: PointVos[]
  tunnelId: string
  tunnelModel: object
  tunnelName: string
}

export interface BaseInfo {
  minX: number
  maxX: number
  minY: number
  maxY: number
  minZ: number
  maxZ: number
  xDrawString: number
  zDrawString: number
}

export interface WireData {
  baseInfo: BaseInfo
  wireItems: WireItem[]
  customerNo: string
}

export interface PointDto {
  h: number
  tunnelPointId: string
  x: number
  y: number
}

export interface TunnelPoint {
  pointDtos?: PointDto[]
  tunnelId?: string
  tunnelName: string
}

const operationConfigurationStore = useOperationConfiguration()
const { customerNo } = storeToRefs(operationConfigurationStore)

export function login(form) {
  return http.postOrJson('/auth/login', form)
}
// 获取人员基站列表
export function getrRyjzList() {
  return http.postOrJson('/jzry/ryjzList', {
    customerNo: customerNo.value
  })
}

// 获取传感器类型字典
export function getSensorTypeDict() {
  return http.get('/dict/sensorTypeDict')
}

// 获取传感器列表
export async function getSensorList() {
  const res = await http.postOrJson('/monitor/sensorList', {
    customerNo: customerNo.value,
    pageSize: -1
  })
  return {
    data: res.data.records
  }
}
// 获取视频列表
export async function getCameraList() {
  const res = await http.postOrJson('/video/cameraList', {
    customerNo: customerNo.value,
    pageSize: -1
  })
  return {
    data: res.data.records
  }
}
// 获取导线数据
export function getTunnelPoint(): Promise<{ data: WireData }> {
  return http.get('/tunnel/tunnelPoint', {
    params: {
      customerNo: customerNo.value
    }
  })
}
// 导入巷道导线数据
export function importPoints(file: File) {
  const param = new FormData()
  param.append('file', file)
  param.append('customerNo', customerNo.value)
  return http.post('/tunnel/importPoints', param)
}

// 导出导线数据
export async function exportPoints() {
  const param = new FormData()
  param.append('customerNo', customerNo.value)
  const res = await http.post('/tunnel/exportPoints', param, {
    responseType: 'blob'
  })
  const blob = new Blob([res], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'
  })
  const link = document.createElement('a')
  link.href = URL.createObjectURL(blob)
  link.download = '导线数据'
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}
// 下载导线数据模版
export async function tunnelExcel() {
  const res = await http.post('/tunnel/download/excelTemplate', null, {
    responseType: 'blob'
  })
  const blob = new Blob([res], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'
  })
  const link = document.createElement('a')
  link.href = URL.createObjectURL(blob)
  link.download = '导线数据模版'
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}

// 更新/删除 导线数据
export function updatePoint(data: TunnelPoint) {
  return http.postOrJson('/tunnel/updatePoint', {
    ...data,
    customerNo: customerNo.value
  })
}

// 保存巷道模型数据
export function saveModel(data: UserInfo) {
  return http.postOrJson('/tunnel/saveModel', {
    customerNo: customerNo.value,
    modelList: data
  })
}

// 获取巷道模型数据
export function getModelData() {
  return http.get('/tunnel/tunnelModel', {
    params: {
      customerNo: customerNo.value
    }
  })
}

// 获取设备类型字典
export function getDeviceTypeDict() {
  return http.get('/dict/deviceTypeDict')
}

// 保存设备标注
export function saveDeviceAnnotation(data) {
  return http.postOrJson('/tunnel/deviceAnnotation', {
    customerNo: customerNo.value,
    ...data
  })
}

// 删除设备标注
export function deleteDeviceAnnotation(id) {
  return http.post('/tunnel/deleteDeviceAnnotation', {
    customerNo: customerNo.value,
    id
  })
}

// 获取设备标注
export function getDeviceAnnotation() {
  return http.get('/tunnel/getDeviceAnnotation', {
    params: {
      customerNo: customerNo.value
    }
  })
}

interface DeviceInfo {
  id: string
  deviceType: string
}
// 获取设备标注详情
export function getDeviceAnnotationDetail(data: DeviceInfo) {
  return http.get('/tunnel/getDeviceAnnotationDetail', {
    params: {
      customerNo: customerNo.value,
      ...data
    }
  })
}

// 下载设备数据模版
export async function deviceExcel() {
  const res = await http.post('/tunnel/download/device/excelTemplate', null, {
    responseType: 'blob'
  })
  const blob = new Blob([res], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'
  })
  const link = document.createElement('a')
  link.href = URL.createObjectURL(blob)
  link.download = '设备数据模版'
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}

// 导入设备数据
export function importDevice(file: File) {
  const param = new FormData()
  param.append('file', file)
  param.append('customerNo', customerNo.value)
  return http.post('/tunnel/importDevice', param)
}

// 导出设备数据
export async function exportDevice() {
  const param = new FormData()
  param.append('customerNo', customerNo.value)
  const res = await http.post('/tunnel/exportDevice', param, {
    responseType: 'blob'
  })
  const blob = new Blob([res], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'
  })
  const link = document.createElement('a')
  link.href = URL.createObjectURL(blob)
  link.download = '设备数据'
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}
